package com.spa.infrastructure.repository.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.CarouselCondition;
import com.spa.infrastructure.repository.mapper.CarouselMapper;
import com.spa.infrastructure.repository.po.CarouselPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class CarouselRepositoryService extends ServiceImpl<CarouselMapper, CarouselPO> {


    public List<CarouselPO> listAll(CarouselCondition condition){
        LambdaQueryWrapper<CarouselPO> wrapper = wrapper(condition);
        return list(wrapper);

    }

    public Page<CarouselPO> page(CarouselCondition condition){
        LambdaQueryWrapper<CarouselPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);

    }


    private LambdaQueryWrapper<CarouselPO> wrapper(CarouselCondition condition) {
        LambdaQueryWrapper<CarouselPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getType()),CarouselPO::getType,condition.getType());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getIsShow()),CarouselPO::getIsShow,condition.getIsShow());

        wrapper.orderByAsc(CarouselPO::getSort);
        return wrapper;
    }

}
